//
//  GroupViewController.m
//  TEXT
//
//  Created by liubo on 2024/3/13.
//  Copyright © 2024 刘博. All rights reserved.
//

#import "GroupViewController.h"

@interface GroupViewController ()

@end

@implementation GroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self twoRequest];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(kkk) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}

- (void)kkk
{
    
}

- (void)twoRequest {
    // 创建 任务组
    dispatch_group_t group = dispatch_group_create();
    // 获取一个全局队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 创建 信号量
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
    // 执行循序1
    dispatch_group_async(group, queue, ^{
        NSLog(@"啦啦啦啦第一个");
        // 第一个请求
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            dispatch_semaphore_signal(semaphore);
            NSLog(@"第一个请求结束了");
        });
             // 执行顺序4/6
    });

    // 执行循序2
    dispatch_group_async(group, queue, ^{
        NSLog(@"啦啦啦啦第二个");
        // 第二个请求
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            dispatch_semaphore_signal(semaphore);
            NSLog(@"哈哈第二个请求结束了");
        });
    });

    dispatch_group_notify(group, queue, ^{
        NSLog(@"哈哈哈进入了进入了");
         // 执行循序3
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
         // 执行顺序5
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
         // 执行顺序7
        dispatch_async(dispatch_get_main_queue(), ^{  NSLog(@"哈哈哈这里这里这里"); });
   });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
